package DBLayer;
import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBSaleLineItem implements IFDBSaleLineItem {
	private Connection con;
	private PreparedStatement pStmtSelect;
	private SaleLineItem sli;
	
	public DBSaleLineItem()
	{
		con = DbConnection.getInstance().getDBcon();
		pStmtSelect = null;
	}
	
	public SaleLineItem findWithPreQuery(int saleOrderId, boolean retriveAssociation)
    {   System.out.println("Find with preCompiled");
        if(pStmtSelect == null){
           pStmtSelect =  getPreparedStmt();          
        }
        SaleLineItem sliObj = null;
        ResultSet results;
        try{
       
        pStmtSelect.setInt(1,saleOrderId);
        results = pStmtSelect.executeQuery();
        if(results.next()){
             sliObj = buildSaleLineItem(results);	
        }
        }
        catch(Exception e){
            e.getMessage();
        }
        return sliObj;
    }
	
	private PreparedStatement getPreparedStmt()
    {
       try{ 
        String query = "select * from SaleLineItem" +
                       " where saleLineItemId = ?";
        System.out.println("Preparestatement " + query);
        pStmtSelect = con.prepareStatement(query);
        
       }
       catch(Exception e){
            e.getMessage();
        }
       return pStmtSelect;
    }
	
	
	
	
	
	public ArrayList<SaleLineItem> getAllSaleLineItem(boolean retriveAssociation) {
		// TODO Auto-generated method stub
		return null;
	}

	
	private SaleLineItem singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		SaleLineItem sliObj = new SaleLineItem();
                
	        String query =  buildQuery(wClause);
                System.out.println(query);
		try{ // read the employee from the database
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
	 			sliObj = buildSaleLineItem(results);
                            //assocaition is to be build
                            stmt.close();
                            
			}
                        else{ //no employee was found
                        	sliObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return sliObj;
	}
	//method to build the query
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM SaleLineItem";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	//method to build an SaleLineItem object
	private SaleLineItem buildSaleLineItem(ResultSet results)
      {   SaleLineItem sliObj = new SaleLineItem();
          try{ // the columns from the table SaleLineItem are used
                sliObj.setItemId(results.getInt("saleLineItemId"));
                sliObj.setQuantity(results.getInt("quantity"));
                sliObj.setSaleOrderId(results.getInt("saleOrderId"));
                }
         catch(Exception e)
         {
             System.out.println("error in building the saleLineItem object");
         }
         return sliObj;
      }


	public int update(SaleLineItem sli) {
		// TODO Auto-generated method stub
		return 0;
				
	}
	public int deleteSaleLineItem(int saleLineItemId) {
		int rc = -1;
		PreparedStatement pstmt = null;
		String delete = "delete from SaleLineItem where saleLineItemId = ?";
		System.out.println(delete);
		try
		{
			pstmt = con.prepareStatement(delete);
            pstmt.setInt(1, saleLineItemId);
            rc = pstmt.executeUpdate();
		}
        catch(SQLException sqlE)
             {
                 System.out.println("SQL Error");
                 System.out.println(sqlE.getMessage());   
             }
             catch(Exception e){
                 e.getMessage();
                 
             }
            return rc;
        }

	public int insertSaleLineItem(SaleLineItem sliObj, boolean retriveAssociation) {
		 int rc = -1;
	        PreparedStatement pstmt = null;
	        String insert = "insert into SaleLineItem(ItemId, quantity, SaleOrderID)" +
	                "values (?,?,?)";
	        System.out.println(insert);
	        try
	        {
	            pstmt = con.prepareStatement(insert);
	            pstmt.setInt(1,sliObj.getItemId());
	            pstmt.setInt(2,sliObj.getQuantity());
	            pstmt.setInt(3,sliObj.getSaleOrderId());   
	            rc = pstmt.executeUpdate();
	        }
	        catch(SQLException sqlE)
	        {    
	             System.out.println("FEJL");  
	             System.out.println(sqlE.getMessage());
	        }
	        catch(Exception e)
	        {
	            e.getMessage();
	        }
	        return rc;
	    }


	public SaleLineItem findItemId(int itemId, boolean retriveAssociation)
	{
		SaleLineItem sliObj = new SaleLineItem();
		sliObj = findWithPreQuery(itemId, retriveAssociation);
		return sliObj;
	}


	public SaleLineItem findSaleOrderId(int saleOrderId, boolean retriveAssociation) {
		SaleLineItem sliObj = new SaleLineItem();
		sliObj = findWithPreQuery(saleOrderId, retriveAssociation);
		return sliObj;
	}
	
}